//
//  UIScrollView+ScreenShot.m
//  Common
//
//  Created by shiqing guo on 2018/9/27.
//  Copyright © 2018年 www.bitkinetic.com. All rights reserved.
//

#import "UIScrollView+ScreenShot.h"

@implementation UIScrollView (ScreenShot)

#pragma mark -- 截屏操作
- (UIImage *)screenShot
{
    // 最后一个参数表示缩放比例
    UIGraphicsBeginImageContextWithOptions(self.contentSize, NO, 0.0);
    
    //保存collectionView当前的偏移量
    CGPoint savedContentOffset = self.contentOffset;
    CGRect saveFrame = self.frame;
    
    //将collectionView的偏移量设置为(0,0)
    self.contentOffset = CGPointZero;
    self.frame = CGRectMake(0, 0, self.contentSize.width, self.contentSize.height);
    
    //need to translate the context down to the current visible portion of the scrollview
    CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0f, -self.contentOffset.y);
    //在当前上下文中渲染出collectionView
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    //截取当前上下文生成Image
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    //恢复collectionView的偏移量
    self.contentOffset = savedContentOffset;
    self.frame = saveFrame;
    
    // 对图片进行压缩，避免过大
    NSData *imageData = UIImageJPEGRepresentation(image, 0.55);
    image = [UIImage imageWithData:imageData];
    
    return image;
}

- (UIImage *) screenshotForScrollViewWithContentOffset:(CGPoint)contentOffset {
    UIGraphicsBeginImageContext(self.bounds.size);
    //need to translate the context down to the current visible portion of the scrollview
    CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0f, -contentOffset.y);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    // helps w/ our colors when blurring
    // feel free to adjust jpeg quality (lower = higher perf)
    NSData *imageData = UIImageJPEGRepresentation(image, 0.55);
    image = [UIImage imageWithData:imageData];
    
    return image;
}

- (UIImage *)screenShotAndAddQrcode
{
    // 最后一个参数表示缩放比例
    UIGraphicsBeginImageContextWithOptions(self.contentSize, NO, 0.0);
    
    //保存collectionView当前的偏移量
    CGPoint savedContentOffset = self.contentOffset;
    CGRect saveFrame = self.frame;
    
    //将collectionView的偏移量设置为(0,0)
    self.contentOffset = CGPointZero;
    self.frame = CGRectMake(0, 0, self.contentSize.width, self.contentSize.height);
    
    //need to translate the context down to the current visible portion of the scrollview
    CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0f, -self.contentOffset.y);
    //在当前上下文中渲染出collectionView
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    //截取当前上下文生成Image
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    //恢复collectionView的偏移量
    self.contentOffset = savedContentOffset;
    self.frame = saveFrame;
    
    // 对图片进行压缩，避免过大
    NSData *imageData = UIImageJPEGRepresentation(image, 0.55);
    image = [UIImage imageWithData:imageData];
//    return image;
    
    UIImage *bottomImg = [UIImage imageNamed:@"qrcode_for_gzh"];
    NSData *bottomData = UIImageJPEGRepresentation(bottomImg, 0.55);
    bottomImg = [UIImage imageWithData:bottomData];
    
    UIImage *result = [self combineWithTopImg:image andBottomImg:bottomImg];
    return result;
}

- (UIImage *)combineWithTopImg:(UIImage *)topImg andBottomImg:(UIImage *)bottomImg
{
    CGFloat width = topImg.size.width;
    CGFloat bottom_h = width * bottomImg.size.height / bottomImg.size.width;
    CGFloat height = topImg.size.height + bottom_h;
    CGSize offScreenSize = CGSizeMake(width, height);
    
    UIGraphicsBeginImageContextWithOptions(offScreenSize, YES, 1);
    CGRect rect = CGRectMake(0, 0, width, topImg.size.height);
    [topImg drawInRect:rect];
    rect.origin.y += topImg.size.height;
    
    CGRect rect1 = CGRectMake(0, rect.origin.y, width, bottom_h);
    [bottomImg drawInRect:rect1];
    
    UIImage *imagez = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return imagez;
}

@end
